package com.my.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * NIO channel2
 * @author Lelezhang
 * @create 2020-07-26 23:21
 */
public class NIOFileChannel2 {
    public static void main(String[] args) throws Exception {
        // 指定内容
        String str = "我爱java,channel2";
        // 创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("channel2.txt");
        // 获取channel
        FileChannel channel = fileOutputStream.getChannel();
        // 创建字节缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 把内容放如缓冲区
        byteBuffer.put(str.getBytes());
        // 放入转为写出了,所以需要反转
        byteBuffer.flip();
        // 将缓冲区文件内容写出到缓冲区
        channel.write(byteBuffer);
        fileOutputStream.close();
    }
}
